// pages/subrogation/marginFlow/index.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        parameter: {
            'navbar': '1',
            'return': '1',
            'title': '收益流水',
            'color': true,
            'class': false
        },
        date: "自定义",
        days: 30,
        dateSelector: app.MultipleSelectDate,
        type: 0,
        waterType: 1,
        dialogOne: false,
        pageH: app.globalData.pageHeight,
        salesmanId: wx.getStorageSync('userInfo').id,
        pageNum: 1,
        scrollTop: 0,
        picUrl: app.imagesURL
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
 
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getHeight1()
        this.formatTime(30)
    },

    /* 获取流收益流水 */
    incomeIncomeFlowData() {
        var that = this,
            pageNum = 1,
            list = [];
        var data = {
            "endDate": that.data.endTime,
            "pageNum": that.data.pageNum,
            "orderNo": '',
            "pageSize": 15,
            "salesmanId": that.data.salesmanId,
            "startDate": that.data.startTime,
            "type": that.data.type
        }
        // /bdb/WorkerBee/incomeIncomeFlowData
        app.basePost(app.U({
            m: 'bdservice',
            c: 'bdb/WorkerBee',
            a: 'incomeIncomeFlowData'
        }), data, res => {
            pageNum = res.data.pageNum;
            if (pageNum == 1) {
                list = res.data.list;
                console.log(pageNum)
            } else {
                list = app.SplitArray(res.data.list ? res.data.list : [], that.data.returned) //分页合并数组
            }
            that.setData({
                returned: list,
                subtotal: res.data.info
            })
        }, (res) => {
            console.log(res)
        })
    },

    /* 扣款详情 */
    debitDetails(id, type) {
        var that = this;
        // /bdb/WorkerBee/EarningsDetails
        app.baseGet(app.U({
            m: 'bdservice',
            c: 'bdb/WorkerBee',
            a: 'EarningsDetails',
            q: {
                id: id,
                type: type
            }
        }), (res) => {}, res => {
            console.log(res)
            that.setData({
                dialogList: res.data.details,
                dialogOne: true
            })
        })
    },

    //  获取高度适配列表高度
    getHeight1() {
        var that = this;
        wx.createSelectorQuery().selectAll('.navbar, .search, .date, .type, .fineHead').boundingClientRect().exec(function (res) {
            console.log(res)
            that.setData({
                listHeight: res[0][0].height + res[0][1].height + res[0][2].height + res[0][3].height + res[0][4].height
            })
        })
    },

    // 滚动到底部加载更多
    lower() {
        var pageNum = Number(this.data.pageNum) + 1;
        this.setData({
            pageNum: pageNum
        })
        this.incomeIncomeFlowData()
    },

    //弹窗
    dialogOne(e) {
        var id = e.currentTarget.dataset.id,
            type = e.currentTarget.dataset.type;
        this.debitDetails(id, type)
    },

    close(){
        this.setData({
            dialogOne: false
        })
    },

    //跳转搜索
    goSearch() {
        wx.navigateTo({
            url: './search/index',
        })
    },

    handleDate: function (e) {
        let index = e.currentTarget.dataset.item;
        this.setData({
            days: Number(index),
        })
        this.formatTime(index)
    },
    bindMultiPickerChange: function (e) {
        var date = e.detail.value
        var startTime = this.data.dateSelector[0][e.detail.value[0]] + '-' + this.data.dateSelector[1][e.detail.value[1]] + '-' + this.data.dateSelector[2][e.detail.value[2]]
        var endTime = this.data.dateSelector[3][e.detail.value[3]] + '-' + this.data.dateSelector[4][e.detail.value[4]] + '-' + this.data.dateSelector[5][e.detail.value[5]]
        this.setData({
            date: this.data.dateSelector[1][e.detail.value[1]] + '-' + this.data.dateSelector[2][e.detail.value[2]] + '-' + this.data.dateSelector[4][e.detail.value[4]] + '-' + this.data.dateSelector[5][e.detail.value[5]],
            multiIndex: e.detail.value,
            days: '',
            startTime: startTime + ' 00:00:00',
            endTime: endTime + ' 00:00:00',
            pageNum: 1,
            returned: ''
        })
        this.incomeIncomeFlowData()
    },
    //时间格式化
    formatTime: function (index, date) {
        var date = new Date().getTime();
        var onDate = new Date(date + 86400000);
        var preDate = new Date(date - (index * 86400000)); //前n天
        this.setData({
            startTime: app.formatdata('yyyy-MM-dd 00:00:00', index != 0 ? preDate : new Date()),
            endTime: app.formatdata('yyyy-MM-dd 00:00:00', onDate),
            pageNum: 1,
            returned: ''
        })
        this.incomeIncomeFlowData()
    },

    screen: function (e) {
        let index = e.currentTarget.dataset.item
        this.setData({
            type: index,
            pageNum: 1,
            returned: ''
        })
        this.incomeIncomeFlowData()
    },
})